package com.workcell.workdata.xthings.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.google.common.collect.Maps;
import com.workcell.workdata.common.core.constant.CommonConstants;
import com.workcell.workdata.common.core.constant.SecurityConstants;
import com.workcell.workdata.common.core.util.R;
import com.workcell.workdata.xthings.api.feign.RemoteCodeService;
import com.workcell.workdata.xthings.dto.ReportInfo;
import com.workcell.workdata.xthings.entity.mqtt.BeltScalesData;
import com.workcell.workdata.xthings.entity.mqtt.FlowMeterData;
import com.workcell.workdata.xthings.entity.mqtt.FreezerData;
import com.workcell.workdata.xthings.entity.mqtt.HeatExchangersData;
import com.workcell.workdata.xthings.entity.mqtt.PowerMeterData;
import com.workcell.workdata.xthings.mapper.mqtt.BeltScalesDataMapper;
import com.workcell.workdata.xthings.mapper.mqtt.FlowMeterDataMapper;
import com.workcell.workdata.xthings.mapper.mqtt.FreezerDataMapper;
import com.workcell.workdata.xthings.mapper.mqtt.HeatExchangersDataMapper;
import com.workcell.workdata.xthings.mapper.mqtt.PowerMeterDataMapper;
import com.workcell.workdata.xthings.service.AutoReportService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
@DS("slave_flow")
@RequiredArgsConstructor
@Slf4j
public class AutoReportServiceImpl implements AutoReportService {

	private final RemoteCodeService remoteCodeService;
	private final FlowMeterDataMapper flowMeterDataMapper;
	private final BeltScalesDataMapper beltScalesDataMapper;
	private final FreezerDataMapper freezerDataMapper;
	private final HeatExchangersDataMapper heatExchangersDataMapper;
	private final PowerMeterDataMapper powerMeterDataMapper;
	private final RedisTemplate redisTemplate;
	private final String powerMeterDataRedisKey = "POWER_METER_DATA_%s";

	private final String fixedParameter = "%02d";

	@Override
	public R autoReportBeltWorkStation(Date date) {

		//获取当前时间30分钟之前的数据
		Date start = DateUtil.offsetMinute(date, -30);
		//报工实体类
		ReportInfo reportInfo = new ReportInfo();
		//创建日期
		reportInfo.setCreateTime(new Date());
		//更新日期
		reportInfo.setUpdateTime(new Date());
		//创建人id
		reportInfo.setCreatorId("1648981458763612162");
		//更新人id
		reportInfo.setUpdaterId("1648981458763612162");
		//创建人名称
		reportInfo.setCreatorName("小思助手");
		//更新人名称
		reportInfo.setUpdaterName("小思助手");
		//逻辑删除
		reportInfo.setDelFlag("0");
		//状态
		reportInfo.setStatus("待审核");
		//收单人id
		reportInfo.setAcquirerId("1636304996759461890");
		//收单人
		reportInfo.setAcquirerName("生产部");
		//租户
		reportInfo.setTenantId("1");
		//报工工站
		String a168026688460088022 = "带式过滤工站";
		reportInfo.setA168026688460088022(a168026688460088022);
		//报工单号   带式过滤工站报工单 + 创建日期（yyyyMMdd）+ 次数

		String key = a168026688460088022 + "报工单" + DateUtil.format(date, DatePattern.PURE_DATE_PATTERN);

			Integer dayTimeData = (Integer) redisTemplate.opsForValue().get(key);
			if(ObjectUtil.isNotEmpty(dayTimeData)){
				redisTemplate.opsForValue().set(key,dayTimeData+=1, 1, TimeUnit.DAYS);
			}else{
				redisTemplate.opsForValue().set(key, 1, 1, TimeUnit.DAYS);
			}
		dayTimeData = (Integer) redisTemplate.opsForValue().get(key);

		String str = String.format(fixedParameter, dayTimeData);

		String a168026687769757045 = key + str;
		reportInfo.setA168026687769757045(a168026687769757045);
		//报工日期
		String a168026689043368640 = DateUtil.formatDate(date);
		reportInfo.setA168026689043368640(a168026689043368640);
		//报工时段
		String a168135100469231484 = DateUtil.format(start, "HH:mm") + "-" + DateUtil.format(date, "HH:mm");
		reportInfo.setA168135100469231484(a168135100469231484);
		//是否白班  8~20 白班  20~8 晚班
		LocalTime localTime = LocalDateTimeUtil.of(date).toLocalTime();
		String a168026689560384944 = localTime.isAfter(LocalTime.of(8, 0, 0)) && (localTime.isBefore(LocalTime.of(20, 0, 0)) || localTime.equals(LocalTime.of(20, 0, 0))) ? "白班" : "晚班";
		reportInfo.setA168026689560384944(a168026689560384944);
		//报工人
		reportInfo.setA168026721919839193("1648981458763612162");
		//报工时间
		reportInfo.setA168026722154053170(DateUtil.formatDateTime(new Date()));

		//设置物料信息
		List<String> deviceNoList = new ArrayList<>();
		//浆化储罐进料
		deviceNoList.add("860059058643425");
		//一段2#卤水罐
		deviceNoList.add("860059054445239");
		String deviceNo = "";
		//目前只有一家厂家
		String manufacturerCode = "ycjtFlowMeter";

		//投入物料
		FlowMeterData inputCharges = flowMeterDataMapper.getDataHourInfo("860059058643425", manufacturerCode, start, date);
		reportInfo.setA168026706350433462(ObjectUtil.isEmpty(inputCharges)?"0":inputCharges.getCumFlowDiffDataHour());
		//产出产品
		FlowMeterData outCharges = flowMeterDataMapper.getDataHourInfo("860059054445239", manufacturerCode, start, date);
		reportInfo.setA168026706586331786(ObjectUtil.isEmpty(outCharges)?"0":outCharges.getCumFlowDiffDataHour());

		//产出副产品
		BeltScalesData outByproduct = beltScalesDataMapper.getDataHourInfo("221219", "czbochuang_BeltScales", start, date);
		reportInfo.setA168026706754995584(ObjectUtil.isEmpty(outByproduct)?"0":outByproduct.getCumulativeWeightDiffDataHour());
		//提供给smartOS数据
		Map<String, Object> map = BeanUtil.beanToMap(reportInfo, true, true);

		//测试
//		map.put("tableName", "F_20230413204005");

		//正式
		map.put("tableName", "f_20230331134029");
		// 推送接口
		R pushResult = remoteCodeService.newSaveWorkReport(map, SecurityConstants.FROM_IN);
		if (CommonConstants.SUCCESS != pushResult.getCode() && ObjectUtil.isNotEmpty(pushResult.getData())) {
			return R.failed("数据推送失败");
		}

		return R.ok("数据提交成功");

	}

	@Override
	public R autoReportDryingKilnStation(Date date) {

		//获取当前时间30分钟之前的数据
		Date start = DateUtil.offsetMinute(date, -30);
		//报工实体类
		ReportInfo reportInfo = new ReportInfo();
		//创建日期
		reportInfo.setCreateTime(new Date());
		//更新日期
		reportInfo.setUpdateTime(new Date());
		//创建人id
		reportInfo.setCreatorId("1648981458763612162");
		//更新人id
		reportInfo.setUpdaterId("1648981458763612162");
		//创建人名称
		reportInfo.setCreatorName("小思助手");
		//更新人名称
		reportInfo.setUpdaterName("小思助手");
		//逻辑删除
		reportInfo.setDelFlag("0");
		//状态
		reportInfo.setStatus("待审核");
		//收单人id
		reportInfo.setAcquirerId("1636304996759461890");
		//收单人
		reportInfo.setAcquirerName("生产部");
		//租户
		reportInfo.setTenantId("1");

		//提供给smartOS数据
		Map<String, Object> map = BeanUtil.beanToMap(reportInfo, true, true);

		//报工工站
		String a168005256041929595 = "烘干窑工站";
		map.put("a168005256041929595",a168005256041929595);
		//报工单号  烘干窑工站报工单 + 创建日期（yyyyMMdd）+ X  + 开始时间（HHmm） +结束时间（HHmm）
		String key = a168005256041929595 + "报工单" + DateUtil.format(date, DatePattern.PURE_DATE_PATTERN);

		Integer dayTimeData = (Integer) redisTemplate.opsForValue().get(key);
		if(ObjectUtil.isNotEmpty(dayTimeData)){
			redisTemplate.opsForValue().set(key,dayTimeData+=1, 1, TimeUnit.DAYS);
		}else{
			redisTemplate.opsForValue().set(key, 1, 1, TimeUnit.DAYS);
		}
		dayTimeData = (Integer) redisTemplate.opsForValue().get(key);

		String str = String.format(fixedParameter, dayTimeData);

		String a168004878936064156 = key + str;

		map.put("a168004878936064156",a168004878936064156);
		//报工日期
		String a16800522112036334 = DateUtil.formatDate(date);
		map.put("a16800522112036334",a16800522112036334);
		//报工时段
		String a168172025002525915 = DateUtil.format(start, "HH:mm") + "-" + DateUtil.format(date, "HH:mm");
		map.put("a168172025002525915",a168172025002525915);
		//是否白班  8~20 白班  20~8 晚班
		LocalTime localTime = LocalDateTimeUtil.of(date).toLocalTime();
		String a168005214995144190 = localTime.isAfter(LocalTime.of(8, 0, 0)) && (localTime.isBefore(LocalTime.of(20, 0, 0)) || localTime.equals(LocalTime.of(20, 0, 0))) ? "白班" : "晚班";
		map.put("a168005214995144190",a168005214995144190);
		//报工人
		String a16800696905389143 = "1648981458763612162";
		map.put("a16800696905389143",a16800696905389143);
		//报工时间
		String a168005211128796588 = DateUtil.formatDateTime(new Date());
		map.put("a168005211128796588",a168005211128796588);

		//产出干云母批次号
		String a168083291166188960 = "干锂云母" + DateUtil.format(date, DatePattern.PURE_DATE_PATTERN) + "X" + DateUtil.format(start, "HHmm") + DateUtil.format(date, "HHmm");
		map.put("a168083291166188960",a168083291166188960);
		//投入主料（湿锂云母）
		BeltScalesData inputCharges = beltScalesDataMapper.getDataHourInfo("221218", "czbochuang_BeltScales", start, date);
		map.put("a168005163455175629",ObjectUtil.isEmpty(inputCharges)?"0":inputCharges.getCumulativeWeightDiffDataHour());
		//产出成品（干锂云母）
		BeltScalesData outByproduct = beltScalesDataMapper.getDataHourInfo("221217", "czbochuang_BeltScales", start, date);
		map.put("a168005163679881663",ObjectUtil.isEmpty(outByproduct)?"0":outByproduct.getCumulativeWeightDiffDataHour());

		//测试
//		map.put("tableName", "F_20230413204001");

		//正式
		map.put("tableName", "f_20230328133001");
		// 推送接口
		R pushResult = remoteCodeService.newSaveWorkReport(map, SecurityConstants.FROM_IN);

		if (CommonConstants.SUCCESS != pushResult.getCode() && ObjectUtil.isNotEmpty(pushResult.getData())) {
			return R.failed("数据推送失败");
		}

		return R.ok("数据提交成功");

	}

	@Override
	public R autoReportMixingStation(Date date) {

		//获取当前时间30分钟之前的数据
		Date start = DateUtil.offsetMinute(date, -30);
		//报工实体类
		ReportInfo reportInfo = new ReportInfo();
		//创建日期
		reportInfo.setCreateTime(new Date());
		//更新日期
		reportInfo.setUpdateTime(new Date());
		//创建人id
		reportInfo.setCreatorId("1648981458763612162");
		//更新人id
		reportInfo.setUpdaterId("1648981458763612162");
		//创建人名称
		reportInfo.setCreatorName("小思助手");
		//更新人名称
		reportInfo.setUpdaterName("小思助手");
		//逻辑删除
		reportInfo.setDelFlag("0");
		//状态
		reportInfo.setStatus("待审核");
		//收单人id
		reportInfo.setAcquirerId("1636304996759461890");
		//收单人
		reportInfo.setAcquirerName("生产部");
		//租户
		reportInfo.setTenantId("1");

		//提供给smartOS数据
		Map<String, Object> map = BeanUtil.beanToMap(reportInfo, true, true);

		//报工工站
		String a16800728187023830 = "混料工站";
		map.put("a16800728187023830",a16800728187023830);
		//报工单号  混料工站报工单 + 创建日期（yyyyMMdd）+ X  + 开始时间（HHmm） +结束时间（HHmm）
		String key = a16800728187023830 + "报工单" + DateUtil.format(date, DatePattern.PURE_DATE_PATTERN);

		Integer dayTimeData = (Integer) redisTemplate.opsForValue().get(key);
		if(ObjectUtil.isNotEmpty(dayTimeData)){
			redisTemplate.opsForValue().set(key,dayTimeData+=1, 1, TimeUnit.DAYS);
		}else{
			redisTemplate.opsForValue().set(key, 1, 1, TimeUnit.DAYS);
		}
		dayTimeData = (Integer) redisTemplate.opsForValue().get(key);

		String str = String.format(fixedParameter, dayTimeData);

		String a16800709000937261 = key + str;
		map.put("a16800709000937261",a16800709000937261);
		//报工日期
		String a168007282281453084 = DateUtil.formatDate(date);
		map.put("a168007282281453084",a168007282281453084);
		//报工时段
		String a168171859195363980 = DateUtil.format(start, "HH:mm") + "-" + DateUtil.format(date, "HH:mm");
		map.put("a168171859195363980",a168171859195363980);
		//是否白班  8~20 白班  20~8 晚班
		LocalTime localTime = LocalDateTimeUtil.of(date).toLocalTime();
		String a168007282721452548 = localTime.isAfter(LocalTime.of(8, 0, 0)) && (localTime.isBefore(LocalTime.of(20, 0, 0)) || localTime.equals(LocalTime.of(20, 0, 0))) ? "白班" : "晚班";
		map.put("a168007282721452548",a168007282721452548);
		//报工人
		String a16800729380256819 = "1648981458763612162";
		map.put("a16800729380256819",a16800729380256819);
		//报工时间
		String a168007294010730458 = DateUtil.formatDateTime(new Date());
		map.put("a168007294010730458",a168007294010730458);

		//产出混合料批次号
		String a168083399811122843 = "混合料-" + DateUtil.format(date, DatePattern.PURE_DATE_PATTERN) + "X" + DateUtil.format(start, "HHmm") + DateUtil.format(date, "HHmm");
		map.put("a168083399811122843",a168083399811122843);
		//投入烘干锂云母
		BeltScalesData inputCharges = beltScalesDataMapper.getDataHourInfo("221221", "czbochuang_BeltScales", start, date);
		String inputChargesNum = ObjectUtil.isEmpty(inputCharges) ? "0" : inputCharges.getCumulativeWeightDiffDataHour();
		map.put("a168007301387889673",inputChargesNum);
		//投入硫酸钠
		BeltScalesData inExcipients1 = beltScalesDataMapper.getDataHourInfo("SY2023051501", "sanyou_BeltScales", start, date);
		String inExcipientsNum1 = ObjectUtil.isEmpty(inExcipients1) ? "0" : inExcipients1.getCumulativeWeightDiffDataHour();
		map.put("a168026551097545071",inExcipientsNum1);
		//投入硫酸钙
		BeltScalesData inExcipients2 = beltScalesDataMapper.getDataHourInfo("SY2023051502", "sanyou_BeltScales", start, date);
		String inExcipientsNum2 = ObjectUtil.isEmpty(inExcipients2) ? "0" : inExcipients2.getCumulativeWeightDiffDataHour();
		map.put("a168026550934565575",inExcipientsNum2);
		//投入碳酸钙
		BeltScalesData inExcipients3 = beltScalesDataMapper.getDataHourInfo("221220", "czbochuang_BeltScales", start, date);
		String inExcipientsNum3 = ObjectUtil.isEmpty(inExcipients3) ? "0" : inExcipients3.getCumulativeWeightDiffDataHour();
		map.put("a168026551756540434",inExcipientsNum3);
		//产出混合料 以上所有料总和
		map.put("a168026564819924829",NumberUtil.add(inputChargesNum,inExcipientsNum1,inExcipientsNum2,inExcipientsNum3));

		//测试
//		map.put("tableName", "F_20230413204002");

		//正式
		map.put("tableName", "f_20230329134011");
		// 推送接口
		R pushResult = remoteCodeService.newSaveWorkReport(map, SecurityConstants.FROM_IN);

		if (CommonConstants.SUCCESS != pushResult.getCode() && ObjectUtil.isNotEmpty(pushResult.getData())) {
			return R.failed("数据推送失败");
		}

		return R.ok("数据提交成功");

	}

	@Override
	public R autoReportFrozenStation(Date date) {

		//获取当前时间1小时之前的数据
		Date start = DateUtil.offsetHour(date, -1);
		//报工实体类
		ReportInfo reportInfo = new ReportInfo();
		//创建日期
		reportInfo.setCreateTime(new Date());
		//更新日期
		reportInfo.setUpdateTime(new Date());
		//创建人id
		reportInfo.setCreatorId("1648981458763612162");
		//更新人id
		reportInfo.setUpdaterId("1648981458763612162");
		//创建人名称
		reportInfo.setCreatorName("小思助手");
		//更新人名称
		reportInfo.setUpdaterName("小思助手");
		//逻辑删除
		reportInfo.setDelFlag("0");
		//状态
		reportInfo.setStatus("待审核");
		//收单人id
		reportInfo.setAcquirerId("1636304996759461890");
		//收单人
		reportInfo.setAcquirerName("生产部");
		//租户
		reportInfo.setTenantId("1");

		//提供给smartOS数据
		Map<String, Object> map = BeanUtil.beanToMap(reportInfo, true, true);

		//报工工站
		String a168040729714476648 = "冷冻工站";
		map.put("a168040729714476648",a168040729714476648);
		//报工单号  冷冻工站报工单 + 创建日期（yyyyMMdd）+ X  + 开始时间（HHmm） +结束时间（HHmm）
		String key = a168040729714476648 + "报工单" + DateUtil.format(date, DatePattern.PURE_DATE_PATTERN);

		Integer dayTimeData = (Integer) redisTemplate.opsForValue().get(key);
		if(ObjectUtil.isNotEmpty(dayTimeData)){
			redisTemplate.opsForValue().set(key,dayTimeData+=1, 1, TimeUnit.DAYS);
		}else{
			redisTemplate.opsForValue().set(key, 1, 1, TimeUnit.DAYS);
		}
		dayTimeData = (Integer) redisTemplate.opsForValue().get(key);

		String str = String.format(fixedParameter, dayTimeData);

		String a168040729101627625 = key + str;

		map.put("a168040729101627625",a168040729101627625);
		//报工日期
		String a168040743187892533 = DateUtil.formatDate(date);
		map.put("a168040743187892533",a168040743187892533);
		//报工时段
		String a168094092093635014 = DateUtil.format(start, "HH:mm") + "-" + DateUtil.format(date, "HH:mm");
		map.put("a168094092093635014",a168094092093635014);
		//是否白班  8~20 白班  20~8 晚班
		LocalTime localTime = LocalDateTimeUtil.of(date).toLocalTime();
		String a168040743366484815 = localTime.isAfter(LocalTime.of(8, 0, 0)) && (localTime.isBefore(LocalTime.of(20, 0, 0)) || localTime.equals(LocalTime.of(20, 0, 0))) ? "白班" : "晚班";
		map.put("a168040743366484815",a168040743366484815);
		//报工人
		String a168040755907768914 = "1648981458763612162";
		map.put("a168040755907768914",a168040755907768914);
		//报工时间
		String a168040756338296643 = DateUtil.formatDateTime(new Date());
		map.put("a168040756338296643",a168040756338296643);

		//冰机出口温度（摄氏度）"冰机出口温度"==冷冻出水温度
		FreezerData inputCharges = freezerDataMapper.getDataHourLineChartInfo("1804A02", "JIUDING", start, date);
//		FreezerData inputCharges = freezerDataMapper.getDataHourLineChartInfo("test_freeze01", "test_fac", start, date);
		map.put("a168126896869490188",ObjectUtil.isEmpty(inputCharges) || ObjectUtil.isNull(inputCharges.getChilledEffluentTemperature())?"0":inputCharges.getChilledEffluentTemperature());
		//冰机入口温度（摄氏度） "冰机入口温度"==冷冻回水温度
		map.put("a168126898878758642",ObjectUtil.isEmpty(inputCharges) || ObjectUtil.isNull(inputCharges.getChilledReturnTemperature())?"0":inputCharges.getChilledReturnTemperature());

		//查询换热机数据
		HeatExchangersData dataHourInfo = heatExchangersDataMapper.getDataHourInfo("TP2023042401", "TAIPING", start, date);
		//投入蒸发母液量（立方）
		map.put("a168040752970499574",ObjectUtil.isEmpty(dataHourInfo) || ObjectUtil.isNull(dataHourInfo)?"0":dataHourInfo.getDifEvaporateMotherLiquor());
		//产出冷冻液（立方）
		map.put("a168048570868754093",ObjectUtil.isEmpty(dataHourInfo) || ObjectUtil.isNull(dataHourInfo)?"0":dataHourInfo.getDifRefrigerationLiquid());

		//查询换热机数据
		HeatExchangersData dataHourLineChartInfo = heatExchangersDataMapper.getDataHourLineChartInfo("TP2023042401", "TAIPING", start, date);
		//加料温度（摄氏度）
		map.put("a168126976124451339",ObjectUtil.isEmpty(dataHourLineChartInfo) || ObjectUtil.isNull(dataHourLineChartInfo)?"0":dataHourLineChartInfo.getDosingTemperature());
		//冷冻罐料温度
		map.put("a168094187341817550",ObjectUtil.isEmpty(dataHourLineChartInfo) || ObjectUtil.isNull(dataHourLineChartInfo)?"0":dataHourLineChartInfo.getFreezerTankTemperature());
		//换热器情况  换热机情况（1：异常， 2：正常）
		map.put("a168094131339728233",ObjectUtil.isEmpty(dataHourLineChartInfo)?"异常":"2".equals(dataHourLineChartInfo.getDeviceStatus())?"正常":"异常");

		//产出芒硝(吨) 无采集
		map.put("a168040752838761133","0");
		//Na浓度（克/升）  无采集
		map.put("a168049077573645334","0");

		//测试
//		map.put("tableName", "F_20230413204012");

		//正式
		map.put("tableName", "f_20230402134036");
		// 推送接口
		R pushResult = remoteCodeService.newSaveWorkReport(map, SecurityConstants.FROM_IN);

		if (CommonConstants.SUCCESS != pushResult.getCode() && ObjectUtil.isNotEmpty(pushResult.getData())) {
			return R.failed("数据推送失败");
		}

		return R.ok("数据提交成功");

	}


	@Override
	public R autoReportRoastingStation(Date date) {

		//获取当前时间1小时之前的数据
		Date start = DateUtil.offsetHour(date, -1);
		//报工实体类
		ReportInfo reportInfo = new ReportInfo();
		//创建日期
		reportInfo.setCreateTime(new Date());
		//更新日期
		reportInfo.setUpdateTime(new Date());
		//创建人id
		reportInfo.setCreatorId("1648981458763612162");
		//更新人id
		reportInfo.setUpdaterId("1648981458763612162");
		//创建人名称
		reportInfo.setCreatorName("小思助手");
		//更新人名称
		reportInfo.setUpdaterName("小思助手");
		//逻辑删除
		reportInfo.setDelFlag("0");
		//状态
		reportInfo.setStatus("待审核");
		//收单人id
		reportInfo.setAcquirerId("1636304996759461890");
		//收单人
		reportInfo.setAcquirerName("生产部");
		//租户
		reportInfo.setTenantId("1");

		//提供给smartOS数据
		Map<String, Object> map = BeanUtil.beanToMap(reportInfo, true, true);

		//报工工站
		String a168007609966362975 = "焙烧工站";
		map.put("a168007609966362975",a168007609966362975);
		//报工单号  焙烧工站报工单 + 创建日期（yyyyMMdd）+ X  + 开始时间（HHmm） +结束时间（HHmm）
		String key = a168007609966362975 + "报工单" + DateUtil.format(date, DatePattern.PURE_DATE_PATTERN);

		Integer dayTimeData = (Integer) redisTemplate.opsForValue().get(key);
		if(ObjectUtil.isNotEmpty(dayTimeData)){
			redisTemplate.opsForValue().set(key,dayTimeData+=1, 1, TimeUnit.DAYS);
		}else{
			redisTemplate.opsForValue().set(key, 1, 1, TimeUnit.DAYS);
		}
		dayTimeData = (Integer) redisTemplate.opsForValue().get(key);

		String str = String.format(fixedParameter, dayTimeData);

		String a168007441448581879 = key + str;

		map.put("a168007441448581879",a168007441448581879);
		//报工日期
		String a168007610579430810 = DateUtil.formatDate(date);
		map.put("a168007610579430810",a168007610579430810);
		//报工时段
		String a168143058748464584 = DateUtil.format(start, "HH:mm") + "-" + DateUtil.format(date, "HH:mm");
		map.put("a168143058748464584",a168143058748464584);
		//是否白班  8~20 白班  20~8 晚班
		LocalTime localTime = LocalDateTimeUtil.of(date).toLocalTime();
		String a168007610970560406 = localTime.isAfter(LocalTime.of(8, 0, 0)) && (localTime.isBefore(LocalTime.of(20, 0, 0)) || localTime.equals(LocalTime.of(20, 0, 0))) ? "白班" : "晚班";
		map.put("a168007610970560406",a168007610970560406);
		//报工人
		String a168007627276087132 = "1648981458763612162";
		map.put("a168007627276087132",a168007627276087132);
		//报工时间
		String a168007629653074396 = DateUtil.formatDateTime(new Date());
		map.put("a168007629653074396",a168007629653074396);

		//产出焙烧料批次号
		String a168083830717631425 = "焙烧料-" + DateUtil.format(date, DatePattern.PURE_DATE_PATTERN) + "X" + DateUtil.format(start, "HHmm") + DateUtil.format(date, "HHmm");
		map.put("a168083830717631425",a168083830717631425);
		//投入主料
		BeltScalesData inputCharges = beltScalesDataMapper.getDataHourInfo("221222", "czbochuang_BeltScales", start, date);
		map.put("a168007632347183534",ObjectUtil.isEmpty(inputCharges)?"0":inputCharges.getCumulativeWeightDiffDataHour());

		//产出主产品
		BeltScalesData outByproduct = beltScalesDataMapper.getDataHourInfo("221223", "czbochuang_BeltScales", start, date);
		map.put("a16800766525024594",ObjectUtil.isEmpty(outByproduct)?"0":outByproduct.getCumulativeWeightDiffDataHour());

		//测试
//		map.put("tableName", "F_20230413204003");

		//正式
		map.put("tableName", "f_20230329134015");
		// 推送接口
		R pushResult = remoteCodeService.newSaveWorkReport(map, SecurityConstants.FROM_IN);

		if (CommonConstants.SUCCESS != pushResult.getCode() && ObjectUtil.isNotEmpty(pushResult.getData())) {
			return R.failed("数据推送失败");
		}

		return R.ok("数据提交成功");

	}

	@Override
	public R autoReportBallMillStation(Date date) {

		//获取当前时间1小时之前的数据
		Date start = DateUtil.offsetHour(date, -1);
		//报工实体类
		ReportInfo reportInfo = new ReportInfo();
		//创建日期
		reportInfo.setCreateTime(new Date());
		//更新日期
		reportInfo.setUpdateTime(new Date());
		//创建人id
		reportInfo.setCreatorId("1648981458763612162");
		//更新人id
		reportInfo.setUpdaterId("1648981458763612162");
		//创建人名称
		reportInfo.setCreatorName("小思助手");
		//更新人名称
		reportInfo.setUpdaterName("小思助手");
		//逻辑删除
		reportInfo.setDelFlag("0");
		//状态
		reportInfo.setStatus("待审核");
		//收单人id
		reportInfo.setAcquirerId("1636304996759461890");
		//收单人
		reportInfo.setAcquirerName("生产部");
		//租户
		reportInfo.setTenantId("1");

		//提供给smartOS数据
		Map<String, Object> map = BeanUtil.beanToMap(reportInfo, true, true);

		//报工工站
		String a168026617436050098 = "球磨工站";
		map.put("a168026617436050098",a168026617436050098);
		//报工单号  球磨工站报工单 + 创建日期（yyyyMMdd）+ X  + 开始时间（HHmm） +结束时间（HHmm）
		String key = a168026617436050098 + "报工单" + DateUtil.format(date, DatePattern.PURE_DATE_PATTERN);

		Integer dayTimeData = (Integer) redisTemplate.opsForValue().get(key);
		if(ObjectUtil.isNotEmpty(dayTimeData)){
			redisTemplate.opsForValue().set(key,dayTimeData+=1, 1, TimeUnit.DAYS);
		}else{
			redisTemplate.opsForValue().set(key, 1, 1, TimeUnit.DAYS);
		}
		dayTimeData = (Integer) redisTemplate.opsForValue().get(key);

		String str = String.format(fixedParameter, dayTimeData);

		String a168026616309112327 = key + str;

		map.put("a168026616309112327",a168026616309112327);
		//报工日期
		String a16802661690242487 = DateUtil.formatDate(date);
		map.put("a16802661690242487",a16802661690242487);
		//报工时段
		String a168240230990258479 = DateUtil.format(start, "HH:mm") + "-" + DateUtil.format(date, "HH:mm");
		map.put("a168240230990258479",a168240230990258479);
		//是否白班  8~20 白班  20~8 晚班
		LocalTime localTime = LocalDateTimeUtil.of(date).toLocalTime();
		String a168026618197883753 = localTime.isAfter(LocalTime.of(8, 0, 0)) && (localTime.isBefore(LocalTime.of(20, 0, 0)) || localTime.equals(LocalTime.of(20, 0, 0))) ? "白班" : "晚班";
		map.put("a168026618197883753",a168026618197883753);
		//报工人
		String a16802667512926005 = "1648981458763612162";
		map.put("a16802667512926005",a16802667512926005);
		//报工时间
		String a168026675388550256 = DateUtil.formatDateTime(new Date());
		map.put("a168026675388550256",a168026675388550256);


		//投入主料
		BeltScalesData inputCharges = beltScalesDataMapper.getDataHourInfo("221223", "czbochuang_BeltScales", start, date);
		map.put("a168026618675847525",ObjectUtil.isEmpty(inputCharges)?"0":inputCharges.getCumulativeWeightDiffDataHour());

		//目前只有一家厂家
		String manufacturerCode = "ycjtFlowMeter";

		//投入辅料
		FlowMeterData outByproduct = flowMeterDataMapper.getDataHourInfo("860059054463323", manufacturerCode, start, date);
		map.put("a168026652851659029",ObjectUtil.isEmpty(outByproduct)?"0":outByproduct.getCumFlowDiffDataHour());

		//产出主产品
		FlowMeterData outCharges = flowMeterDataMapper.getDataHourInfo("860059058643425", manufacturerCode, start, date);
		map.put("a168026618894778805",ObjectUtil.isEmpty(outCharges)?"0":outCharges.getCumFlowDiffDataHour());

		//测试
//		map.put("tableName", "f_20230413204004");

		//正式
		map.put("tableName", "f_20230331134028");
		// 推送接口
		R pushResult = remoteCodeService.newSaveWorkReport(map, SecurityConstants.FROM_IN);

		if (CommonConstants.SUCCESS != pushResult.getCode() && ObjectUtil.isNotEmpty(pushResult.getData())) {
			return R.failed("数据推送失败");
		}

		return R.ok("数据提交成功");

	}

	public R autoReportPowerMeterData(Date date) {
		Map<String, Object> map = Maps.newHashMap();
		Integer unit = 60;
		map.put("tableName","f_20230324130012");

//		map.put("id","1647991772118794240");
		//累计电能：当前时间——开始时间
		String split =  DateUtil.format(date, DatePattern.PURE_DATE_PATTERN);
		DateTime startTime = DateUtil.offsetMinute(date, -5);
		//0-8点取前一天表的数据；否则就取当天的
		String nightEightTime = null;
		String tableNameNow = "t_power_meter_data_"+split;
		LocalTime localTime = LocalDateTimeUtil.of(date).toLocalTime();
		if ((localTime.isAfter(LocalTime.of(0, 0, 0))  || localTime.equals(LocalTime.of(0, 0, 0)))
				&& (localTime.isBefore(LocalTime.of(8, 0, 0)) || localTime.equals(LocalTime.of(8, 0, 0)))) {
			DateTime dateTime = DateUtil.offsetDay(date, -1);
			nightEightTime = DateUtil.formatDate(dateTime) +" 20:00:00";
			split =  DateUtil.format(dateTime, DatePattern.PURE_DATE_PATTERN);
		}else{
			nightEightTime = DateUtil.formatDate(startTime) +" 20:00:00";
		}
		String tableName = "t_power_meter_data_" + split;

		String a168316691888152692 = StrUtil.format("一车间设备报工{}{}",split,"01");

		map.put("a168316691888152692",a168316691888152692);
		map.put("tenant_id","1");
		//查询当前数据
		R data = remoteCodeService.queryXthingsTableData(map,SecurityConstants.FROM_IN);
		if (CommonConstants.SUCCESS != data.getCode() && ObjectUtil.isNotEmpty(data.getData())) {
			return R.failed("数据查询失败");
		}

		map.put("a168230186313661613",DateUtil.formatDateTime(date));

		//回转窑
		//记录白班最先8：00的一条记录
		PowerMeterData dayShiftRotaryKilnData = powerMeterDataMapper.getDataByStartTime(tableName,"sgu_PowerMeter","PowerMeter802512", DateUtil.format(startTime, DatePattern.NORM_DATE_PATTERN)+" 08:00");//
		//记录晚班最先20：00的一条记录
		PowerMeterData nightShiftRotaryKilnData = powerMeterDataMapper.getDataByStartTime(tableName,"sgu_PowerMeter","PowerMeter802512", nightEightTime);//
		//查询最新记录
		PowerMeterData rotaryKilnData = powerMeterDataMapper.getFiveData(tableNameNow, "sgu_PowerMeter","PowerMeter802512", startTime, date);

		//冷却窑
		//记录白班最先8：00的一条记录
		PowerMeterData dayShiftCoolingKilnData = powerMeterDataMapper.getDataByStartTime(tableName,"sgu_PowerMeter","PowerMeter802579", DateUtil.format(startTime, DatePattern.NORM_DATE_PATTERN)+" 08:00");//
		//记录晚班最先20：00的一条记录
		PowerMeterData nightShiftCoolingKilnData = powerMeterDataMapper.getDataByStartTime(tableName,"sgu_PowerMeter","PowerMeter802579", nightEightTime);//
		//查询最新记录
		PowerMeterData coolingKilnData = powerMeterDataMapper.getFiveData(tableNameNow, "sgu_PowerMeter","PowerMeter802579", startTime, date);

		//烘干机
		//记录白班最先8：00的一条记录
		PowerMeterData dayShiftDryerData = powerMeterDataMapper.getDataByStartTime(tableName,"24","24", DateUtil.format(startTime, DatePattern.NORM_DATE_PATTERN)+" 08:00");//
		//记录晚班最先20：00的一条记录
		PowerMeterData nightShiftDryerData= powerMeterDataMapper.getDataByStartTime(tableName,"24","24", nightEightTime);//
		//查询最新记录
		PowerMeterData dryerData = powerMeterDataMapper.getFiveData(tableNameNow, "24","24", startTime, date);

		//判断是否白班 晚班
		String shift = localTime.isAfter(LocalTime.of(8, 0, 0)) && (localTime.isBefore(LocalTime.of(20, 0, 0)) || localTime.equals(LocalTime.of(20, 0, 0))) ? "白班" : "晚班";
		if("白班".equals(shift)){
			String type = "day";
			Integer dayShiftGatherTime = (Integer) redisTemplate.opsForValue().get(String.format(powerMeterDataRedisKey, split+"_DAYSHIFTGATHERTIME"));
			if(ObjectUtil.isNotEmpty(dayShiftGatherTime)){
				redisTemplate.opsForValue().set(String.format(powerMeterDataRedisKey, split+"_DAYSHIFTGATHERTIME"),dayShiftGatherTime+=5, 2, TimeUnit.DAYS);
			}else{
				redisTemplate.opsForValue().set(String.format(powerMeterDataRedisKey, split+"_DAYSHIFTGATHERTIME"), 5, 2, TimeUnit.DAYS);
			}
			dayShiftGatherTime = (Integer) redisTemplate.opsForValue().get(String.format(powerMeterDataRedisKey, split+"_DAYSHIFTGATHERTIME"));

			String machineType = "rotary";
			//回转窑
			String rotaryStatus = packageData(split, rotaryKilnData, type, machineType);
			//运行状态
			map.put("a168189263327610616",rotaryStatus);
			//白班累计电能
			BigDecimal rotaryKilnStartTaee = new BigDecimal(ObjectUtil.isEmpty(dayShiftRotaryKilnData) || ObjectUtil.isEmpty(dayShiftRotaryKilnData.getTaee()) ? "0" : dayShiftRotaryKilnData.getTaee());
			BigDecimal num = new BigDecimal(ObjectUtil.isEmpty(rotaryKilnData) || ObjectUtil.isEmpty(rotaryKilnData.getTaee()) ? "0" : rotaryKilnData.getTaee());
			if(rotaryKilnStartTaee.compareTo(num) <= 0){
				map.put("a168189300647536316",num.subtract(rotaryKilnStartTaee));
			}
			//白班稼动率
			Object runObj = redisTemplate.opsForValue().get(String.format(powerMeterDataRedisKey, split + "_" + type + "SHIFTRUNTIME" + machineType));
			Integer runInt = ObjectUtil.isEmpty(runObj) ? 0 : (Integer) runObj;
			map.put("a168189329781761300",NumberUtil.div(runInt,dayShiftGatherTime,2));
			//白班开机时长
			map.put("a16818941954402310", NumberUtil.div(runInt,unit,2));
			//白班停机时长
			Object stopObj = redisTemplate.opsForValue().get(String.format(powerMeterDataRedisKey, split + "_" + type + "SHIFTSHUTDOWNTIME" + machineType));
			Integer stopInt = ObjectUtil.isEmpty(stopObj) ? 0 : (Integer) stopObj;
			map.put("a16818942738486879",NumberUtil.div(stopInt,unit,2));


			//冷却窑
			machineType = "cooling";
			String coolingStatus = packageData(split, coolingKilnData, type, machineType);
			//运行状态
			map.put("a168189444737837944",coolingStatus);
			//白班累计电能
			BigDecimal coolingKilnStartTaee = new BigDecimal(ObjectUtil.isEmpty(dayShiftCoolingKilnData) || ObjectUtil.isEmpty(dayShiftCoolingKilnData.getTaee()) ? "0" : dayShiftCoolingKilnData.getTaee());
			BigDecimal numCooling = new BigDecimal(ObjectUtil.isEmpty(coolingKilnData) || ObjectUtil.isEmpty(coolingKilnData.getTaee()) ? "0" :coolingKilnData.getTaee());
			if(coolingKilnStartTaee.compareTo(numCooling) <= 0){
				map.put("a168197896211447538",numCooling.subtract(coolingKilnStartTaee));
			}
			//白班稼动率
			Object coolingRunObj = redisTemplate.opsForValue().get(String.format(powerMeterDataRedisKey, split + "_" + type + "SHIFTRUNTIME" + machineType));
			Integer coolingRunInt = ObjectUtil.isEmpty(coolingRunObj) ? 0 : (Integer)coolingRunObj;
			map.put("a168197899093549248",NumberUtil.div(coolingRunInt,dayShiftGatherTime,2));
			//白班开机时长
			map.put("a168197907148850470",NumberUtil.div(coolingRunInt,unit,2));
			//白班停机时长
			Object coolingStopObj = redisTemplate.opsForValue().get(String.format(powerMeterDataRedisKey, split + "_" + type + "SHIFTSHUTDOWNTIME" + machineType));
			Integer coolingStopInt = ObjectUtil.isEmpty(coolingStopObj) ? 0 : (Integer)coolingStopObj;
			map.put("a168197910697292607",NumberUtil.div(coolingStopInt,unit,2));

			//烘干机
			machineType = "dryer";
			String dryerStatus = packageData(split, dryerData, type, machineType);
			//运行状态
			map.put("a168215356491346572",dryerStatus);
			//白班累计电能
			BigDecimal dryerStartTaee = new BigDecimal(ObjectUtil.isEmpty(dayShiftDryerData) || ObjectUtil.isEmpty(dayShiftDryerData.getTaee()) ? "0" : dayShiftDryerData.getTaee());
			BigDecimal numDryer = new BigDecimal(ObjectUtil.isEmpty(dryerData) || ObjectUtil.isEmpty(dryerData.getTaee()) ? "0" : dryerData.getTaee());
			if(dryerStartTaee.compareTo(numDryer) <= 0){
				map.put("a168215361594272146",numDryer.subtract(dryerStartTaee));
			}
			//白班稼动率
			Object dryerRunObj = redisTemplate.opsForValue().get(String.format(powerMeterDataRedisKey, split + "_" + type + "SHIFTRUNTIME" + machineType));
			Integer dryerInt = ObjectUtil.isEmpty(dryerRunObj) ? 0 : (Integer) dryerRunObj;
			map.put("a168215367430385069",NumberUtil.div(dryerInt,dayShiftGatherTime, 2));
			//白班开机时长
			map.put("a168215372659179941",NumberUtil.div(dryerInt,unit,2));
			//白班停机时长
			Object dryerStopObj = redisTemplate.opsForValue().get(String.format(powerMeterDataRedisKey, split + "_" + type + "SHIFTSHUTDOWNTIME" + machineType));
			Integer dryerStopInt = ObjectUtil.isEmpty(dryerStopObj) ? 0 : (Integer) dryerStopObj;
			map.put("a168215373162630168",NumberUtil.div(dryerStopInt,unit,2));

		}else{

			String type = "night";
			Integer nightShiftGatherTime = (Integer) redisTemplate.opsForValue().get(String.format(powerMeterDataRedisKey, split+"_NIGHTSHIFTGATHERTIME"));
			if(ObjectUtil.isNotEmpty(nightShiftGatherTime)){
				redisTemplate.opsForValue().set(String.format(powerMeterDataRedisKey, split+"_NIGHTSHIFTGATHERTIME"),nightShiftGatherTime+=5,2, TimeUnit.DAYS);
			}else{
				redisTemplate.opsForValue().set(String.format(powerMeterDataRedisKey, split+"_NIGHTSHIFTGATHERTIME"),5,2, TimeUnit.DAYS);
			}
			nightShiftGatherTime = (Integer) redisTemplate.opsForValue().get(String.format(powerMeterDataRedisKey, split+"_NIGHTSHIFTGATHERTIME"));

			//回转窑
			String machineType = "rotary";
			String rotaryStatus = packageData(split, rotaryKilnData, type, machineType);
			//运行状态
			map.put("a168189263327610616",rotaryStatus);
			//晚班累计电能
			BigDecimal rotaryKilnStartTaee = new BigDecimal(ObjectUtil.isEmpty(nightShiftRotaryKilnData) || ObjectUtil.isEmpty(nightShiftRotaryKilnData.getTaee()) ? "0" : nightShiftRotaryKilnData.getTaee());
			BigDecimal num = new BigDecimal(ObjectUtil.isEmpty(rotaryKilnData) || ObjectUtil.isEmpty(rotaryKilnData.getTaee()) ? "0" : rotaryKilnData.getTaee());
			if(rotaryKilnStartTaee.compareTo(num) <= 0){
				map.put("a168230198532541622",num.subtract(rotaryKilnStartTaee));
			}
			//晚班稼动率
			Object rotaryRunObj = redisTemplate.opsForValue().get(String.format(powerMeterDataRedisKey, split + "_" + type + "SHIFTRUNTIME" + machineType));
			Integer rotaryRunInt = ObjectUtil.isEmpty(rotaryRunObj) ? 0 : (Integer) rotaryRunObj;
			map.put("a168230198866636418",NumberUtil.div(rotaryRunInt,nightShiftGatherTime,2));
			//晚班开机时长
			map.put("a168230199041624639",NumberUtil.div(rotaryRunInt,unit,2));
			//晚班停机时长
			Object rotaryStopObj = redisTemplate.opsForValue().get(String.format(powerMeterDataRedisKey, split + "_" + type + "SHIFTSHUTDOWNTIME" + machineType));
			Integer rotaryStopInt = ObjectUtil.isEmpty(rotaryStopObj) ? 0 : (Integer) rotaryStopObj;
			map.put("a168230199251683193",NumberUtil.div(rotaryStopInt,unit,2));


			//冷却窑
			machineType = "cooling";
			String coolingStatus = packageData(split, coolingKilnData, type, machineType);
			//运行状态
			map.put("a168189444737837944",coolingStatus);
			//晚班累计电能
			BigDecimal coolingKilnStartTaee = new BigDecimal(ObjectUtil.isEmpty(nightShiftCoolingKilnData) || ObjectUtil.isEmpty(nightShiftCoolingKilnData.getTaee()) ? "0" : nightShiftCoolingKilnData.getTaee());
			BigDecimal numCooling = new BigDecimal(ObjectUtil.isEmpty(coolingKilnData) || ObjectUtil.isEmpty(coolingKilnData.getTaee()) ? "0" : coolingKilnData.getTaee());
			if(coolingKilnStartTaee.compareTo(numCooling) <= 0){
				map.put("a168230204496679638",numCooling.subtract(coolingKilnStartTaee));
			}
			//晚班稼动率
			Object coolingRunObj = redisTemplate.opsForValue().get(String.format(powerMeterDataRedisKey, split + "_" + type + "SHIFTRUNTIME" + machineType));
			Integer coolingRunInt = ObjectUtil.isEmpty(coolingRunObj) ? 0 : (Integer) coolingRunObj;
			map.put("a168230204721696528",NumberUtil.div(coolingRunInt,nightShiftGatherTime,2));
			//晚班开机时长
			map.put("a168230204939922775",NumberUtil.div(coolingRunInt,unit,2));
			//晚班停机时长
			Object coolingStopObj = redisTemplate.opsForValue().get(String.format(powerMeterDataRedisKey, split + "_" + type + "SHIFTSHUTDOWNTIME" + machineType));
			Integer coolingStopInt = ObjectUtil.isEmpty(coolingStopObj) ? 0 : (Integer) coolingStopObj;
			map.put("a168230205118273434",NumberUtil.div(coolingStopInt,unit,2));

			//烘干机
			machineType = "dryer";
			String dryerStatus = packageData(split, dryerData, type, machineType);
			//运行状态
			map.put("a168215356491346572",dryerStatus);
			//晚班累计电能
			BigDecimal dryerStartTaee = new BigDecimal(ObjectUtil.isEmpty(nightShiftDryerData) || ObjectUtil.isEmpty(nightShiftDryerData.getTaee()) ? "0" :nightShiftDryerData.getTaee());
			BigDecimal numDryer = new BigDecimal(ObjectUtil.isEmpty(dryerData) || ObjectUtil.isEmpty(dryerData.getTaee()) ? "0" : dryerData.getTaee());
			if(dryerStartTaee.compareTo(numDryer) <= 0){
				map.put("a168230215134966833",numDryer.subtract(dryerStartTaee));
			}
			//晚班稼动率
			Object dryerRunObj = redisTemplate.opsForValue().get(String.format(powerMeterDataRedisKey, split + "_" + type + "SHIFTRUNTIME" + machineType));
			Integer dryerRunInt = ObjectUtil.isEmpty(dryerRunObj) ? 0 : (Integer) dryerRunObj;
			map.put("a16823021530492613",NumberUtil.div(dryerRunInt,nightShiftGatherTime, 2));
			//晚班开机时长
			map.put("a168230215479884972",NumberUtil.div(dryerRunInt,unit,2));
			//晚班停机时长
			Object dryerStopObj = redisTemplate.opsForValue().get(String.format(powerMeterDataRedisKey, split + "_" + type + "SHIFTSHUTDOWNTIME" + machineType));
			Integer dryerStopInt = ObjectUtil.isEmpty(dryerStopObj) ? 0 : (Integer) dryerStopObj;
			map.put("a168230215831569635",NumberUtil.div(dryerStopInt,unit,2));
		}

		//查询数据为空 保存
		if (ObjectUtil.isEmpty(data.getData())) {
			//报工实体类
			ReportInfo reportInfo = new ReportInfo();
			//创建日期
			reportInfo.setCreateTime(new Date());
			//更新日期
			reportInfo.setUpdateTime(new Date());
			//创建人id
			reportInfo.setCreatorId("1648981458763612162");
			//更新人id
			reportInfo.setUpdaterId("1648981458763612162");
			//创建人名称
			reportInfo.setCreatorName("小思助手");
			//更新人名称
			reportInfo.setUpdaterName("小思助手");
			//逻辑删除
			reportInfo.setDelFlag("0");
			//状态
			reportInfo.setStatus("待审核");
			//收单人id
			reportInfo.setAcquirerId("1636304996759461890");
			//收单人
			reportInfo.setAcquirerName("生产部");
			//租户
			reportInfo.setTenantId("1");

			//提供给smartOS数据
			Map<String, Object> mapInfo = BeanUtil.beanToMap(reportInfo, true, true);
			map.remove("tenant_id");
			map.putAll(mapInfo);
			try {
				R pushResult = remoteCodeService.newSaveWorkReport(map, SecurityConstants.FROM_IN);
				if (CommonConstants.SUCCESS != pushResult.getCode() && ObjectUtil.isNotEmpty(pushResult.getData())) {
					return R.failed("数据推送失败");
				}
			} catch (Exception e) {
			    e.printStackTrace();
			}
		}else {
			JSON parse = JSONUtil.parse(data.getData());
			Object id = parse.getByPath("id");
			map.put("id",id);
			try {
				R pushResult = remoteCodeService.newUpdateWorkReport(map, SecurityConstants.FROM_IN);
				if (CommonConstants.SUCCESS != pushResult.getCode() && ObjectUtil.isNotEmpty(pushResult.getData())) {
					return R.failed("数据推送失败");
				}
			} catch (Exception e) {
			    e.printStackTrace();
			}
		}

		return R.ok();
	}

	private String packageData(String split,PowerMeterData powerMeterData,String type,String machineType) {
		if (ObjectUtil.isNotEmpty(powerMeterData)) {
			if (StringUtils.isNotEmpty(powerMeterData.getIa()) || StringUtils.isNotEmpty(powerMeterData.getIb()) || StringUtils.isNotEmpty(powerMeterData.getIc())) {
				//记录总运作时间
				Integer shiftRunTime = (Integer) redisTemplate.opsForValue().get(String.format(powerMeterDataRedisKey, split + "_" + type + "SHIFTRUNTIME" + machineType));
				if(ObjectUtil.isNotEmpty(shiftRunTime)){
					redisTemplate.opsForValue().set(String.format(powerMeterDataRedisKey, split + "_" + type + "SHIFTRUNTIME" + machineType), shiftRunTime+=5, 2, TimeUnit.DAYS);
				}else{
					redisTemplate.opsForValue().set(String.format(powerMeterDataRedisKey, split + "_" + type + "SHIFTRUNTIME" + machineType), 5, 2, TimeUnit.DAYS);
				}
				return "运行";
			} else if ("0.00".equals(powerMeterData.getIa()) && "0.00".equals(powerMeterData.getIb()) && "0.00".equals(powerMeterData.getIc())) {
				//记录停机时长
				Integer shiftShutdownTime = (Integer) redisTemplate.opsForValue().get(String.format(powerMeterDataRedisKey, split + "_" + type + "SHIFTSHUTDOWNTIME" + machineType));
				if(ObjectUtil.isNotEmpty(shiftShutdownTime)){
					redisTemplate.opsForValue().set(String.format(powerMeterDataRedisKey, split + "_" + type + "SHIFTSHUTDOWNTIME" + machineType), shiftShutdownTime+=5,2, TimeUnit.DAYS);
				}else{
					redisTemplate.opsForValue().set(String.format(powerMeterDataRedisKey, split + "_" + type + "SHIFTSHUTDOWNTIME" + machineType), 5,2, TimeUnit.DAYS);
				}

				return "停机";
			}
		}else {
			Integer shiftShutdownTime = (Integer) redisTemplate.opsForValue().get(String.format(powerMeterDataRedisKey, split + "_" + type + "SHIFTSHUTDOWNTIME" + machineType));
			if(ObjectUtil.isNotEmpty(shiftShutdownTime)){
				redisTemplate.opsForValue().set(String.format(powerMeterDataRedisKey, split + "_" + type + "SHIFTSHUTDOWNTIME" + machineType), shiftShutdownTime+=5,2, TimeUnit.DAYS);
			}else{
				redisTemplate.opsForValue().set(String.format(powerMeterDataRedisKey, split + "_" + type + "SHIFTSHUTDOWNTIME" + machineType), 5,2, TimeUnit.DAYS);
			}
			return "停机";
		}
		return "停机";
	}


	@Override
	public R autoReportSmartRoastingStation(Date date) {

		//获取当前时间1小时之前的数据
		Date start = DateUtil.offsetHour(date, -1);
		//报工实体类
		ReportInfo reportInfo = new ReportInfo();
		//创建日期
		reportInfo.setCreateTime(new Date());
		//更新日期
		reportInfo.setUpdateTime(new Date());
		//创建人id
		reportInfo.setCreatorId("1648981458763612162");
		//更新人id
		reportInfo.setUpdaterId("1648981458763612162");
		//创建人名称
		reportInfo.setCreatorName("小思助手");
		//更新人名称
		reportInfo.setUpdaterName("小思助手");
		//逻辑删除
		reportInfo.setDelFlag("0");
		//状态
		reportInfo.setStatus("待审核");
		//收单人id
		reportInfo.setAcquirerId("1636304996759461890");
		//收单人
		reportInfo.setAcquirerName("生产部");
		//租户
		reportInfo.setTenantId("1");

		//提供给smartOS数据
		Map<String, Object> map = BeanUtil.beanToMap(reportInfo, true, true);

		//报工工站
		String work_reporting_station = "焙烧工站";
		map.put("work_reporting_station",work_reporting_station);
		//报工单号  焙烧工站报工单 + 创建日期（yyyyMMdd）+ X  + 开始时间（HHmm） +结束时间（HHmm）
		String key = work_reporting_station + "报工单" + DateUtil.format(date, DatePattern.PURE_DATE_PATTERN);

		Integer dayTimeData = (Integer) redisTemplate.opsForValue().get(key);
		if(ObjectUtil.isNotEmpty(dayTimeData)){
			redisTemplate.opsForValue().set(key,dayTimeData+=1, 1, TimeUnit.DAYS);
		}else{
			redisTemplate.opsForValue().set(key, 1, 1, TimeUnit.DAYS);
		}
		dayTimeData = (Integer) redisTemplate.opsForValue().get(key);

		String str = String.format(fixedParameter, dayTimeData);

		String work_order_number = key + str;

		map.put("work_order_number",work_order_number);
		//报工日期
		String date_of_work_application = DateUtil.formatDate(date);
		map.put("date_of_work_application",date_of_work_application);
		//报工时段
		String work_reporting_period = DateUtil.format(start, "HH:mm") + "-" + DateUtil.format(date, "HH:mm");
		map.put("work_reporting_period",work_reporting_period);
		//是否白班  8~20 白班  20~8 晚班 报工班次
		LocalTime localTime = LocalDateTimeUtil.of(date).toLocalTime();
		String reporting_shift = localTime.isAfter(LocalTime.of(8, 0, 0)) && (localTime.isBefore(LocalTime.of(20, 0, 0)) || localTime.equals(LocalTime.of(20, 0, 0))) ? "白班" : "晚班";
		map.put("reporting_shift",reporting_shift);
		//报工人
		String reporting_workers = "1650339611887026177";
		map.put("reporting_workers",reporting_workers);
		//报工时间
		String reporting_time = DateUtil.formatDateTime(new Date());
		map.put("reporting_time",reporting_time);

		//产出焙烧料批次号
		String out_of_produced_materials = "焙烧料-" + DateUtil.format(date, DatePattern.PURE_DATE_PATTERN) + "X" + DateUtil.format(start, "HHmm") + DateUtil.format(date, "HHmm");
		map.put("out_of_produced_materials",out_of_produced_materials);
		//投入主料
		BeltScalesData inputCharges = beltScalesDataMapper.getDataHourInfo("221222", "czbochuang_BeltScales", start, date);
		map.put("input_main_materials",ObjectUtil.isEmpty(inputCharges)?"0":inputCharges.getCumulativeWeightDiffDataHour());

		//产出主产品
		BeltScalesData outByproduct = beltScalesDataMapper.getDataHourInfo("221223", "czbochuang_BeltScales", start, date);
		map.put("output_main_product",ObjectUtil.isEmpty(outByproduct)?"0":outByproduct.getCumulativeWeightDiffDataHour());

		//测试
//		map.put("tableName", "F_20230413204003");

		//正式
		map.put("tableName", "f_20230531246002");
		// 推送接口
		R pushResult = remoteCodeService.newSaveWorkReport(map, SecurityConstants.FROM_IN);

		if (CommonConstants.SUCCESS != pushResult.getCode() && ObjectUtil.isNotEmpty(pushResult.getData())) {
			return R.failed("数据推送失败");
		}

		return R.ok("数据提交成功");
	}

}
